package com.lanou3g.mynewstest.module.topic;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseFragment;
import com.lanou3g.mynewstest.beans.TopicBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;

/**
 * Created by dllo on 16/1/23.
 */
public class TopicFragment extends MyBaseFragment {
    private ListView listView;
    private TopicBeans beans;
    private String url = "http://c.3g.163.com/newstopic/list/expert/0-10.html";
    private TopicAdapter adapter;
    private Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_topic, container, false);
        listView = (ListView) view.findViewById(R.id.topic_lv);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 101) {
                    beans = (TopicBeans) msg.obj;
                    adapter = new TopicAdapter(context, beans);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(context,TopicDetailActivity.class);
                            intent.putExtra("expertId",beans.getData().getExpertList().get(position).getExpertId());
                            context.startActivity(intent);
                        }
                    });
                }
                return false;
            }
        });
        MyRequestQueue.getBeans(TopicBeans.class, url, handler);
        return view;
    }


}
